package dev.android.yeqh.tvannounce.common;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import dev.android.yeqh.tvannounce.BaseActivity;
import dev.android.yeqh.tvannounce.model.NetTaskParam;

public class NetTask extends AsyncTask<NetTaskParam, Long, Object>{
	BaseActivity context = null; 
	ProgressDialog pdialog = null;

	public NetTask(BaseActivity context) {
		this.context = context;
		
		pdialog = new ProgressDialog(context);
		pdialog.setTitle("Loading...");
        pdialog.setCancelable(true);
        pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pdialog.show();

	}
	
	@Override
	protected Object doInBackground(NetTaskParam... params) {
		Object result = null;
		
		try {
			switch (params[0].getTaskType()) {
			case Constants.NET_TASK_GET_AREAS:
				result = Utils.getAreas();
				break;
			case Constants.NET_TASK_GET_CHANNELS:
				result = Utils.getChannels((String)params[0].getParam());
				break;
			case Constants.NET_TASK_GET_SUBCHANNELS:
				if ("SATVLIST".equals((String)params[0].getParam())) {
					result = Utils.getSaTVs();
				} else {
					result = Utils.getSubChannels((String)params[0].getParam());
				}
				break;
			case Constants.NET_TASK_GET_PROGRAMS:
				result = Utils.getPrograms((String)params[0].getParam());
				break;
			default:
				break;
			}
		} catch (Exception e) {
			result = e;
		}

		return result;
	}

	@Override
	protected void onPostExecute(Object result) {
		context.updateUI(result);
		pdialog.dismiss();
		
		super.onPostExecute(result);
	}
	
	
}
